package com.zhbcm.socket.handler;

import java.io.*;
import java.net.Socket;
import java.util.Calendar;

/**
 * @author 朝花不迟暮
 * @version 1.0
 * @date 2021/4/23 20:25
 */
public class TimeServerHandler implements Runnable
{
    private final Socket clientProxy;

    public TimeServerHandler(Socket clientProxy)
    {
        this.clientProxy = clientProxy;
    }

    @Override
    public void run()
    {
        BufferedReader reader = null;
        PrintWriter writer = null;
        try
        {
            reader = new BufferedReader(new InputStreamReader(clientProxy.getInputStream()));
            writer = new PrintWriter(clientProxy.getOutputStream());
            while (true)
            {//因为一个client可以发送多次请求，这里的每一次循环，相当于接收处理一次请求
                String request = reader.readLine();
                if (!"GET CURRENT TIME".equals(request))
                {
                    writer.println("BAD_REQUEST");
                } else
                {
                    writer.println(Calendar.getInstance().getTime().toLocaleString());
                }
                writer.flush();
            }
        } catch (IOException e)
        {
            e.printStackTrace();
        } finally
        {
            try
            {
                assert writer != null;
                writer.close();
                reader.close();
                clientProxy.close();
            } catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }
}
